package com.axend.aerosense.room.ui.fragment.vipfun;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.axend.aerosense.room.entity.IntelligentLearningBean;
import com.axend.aerosense.room.entity.RoomVipSetBean;
import com.blankj.utilcode.util.LogUtils;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.ui.BottomTextPickerDialog;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomFragmentSleepTimeoutBinding;

import java.util.Map;

/**
 * 卧床不起
 * Author: ChenHongWei
 * Date: 2021/3/24 17:47
 */
public class RoomSleepTimeoutFragment extends BaseVipFunFragment<RoomFragmentSleepTimeoutBinding> {

    private Map<String, Float> timeMap;
    private int selectValue;
    private String[] timeList;

    public static RoomSleepTimeoutFragment getInstance(){
        return new RoomSleepTimeoutFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.room_fragment_sleep_timeout;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onChange(CompoundButton buttonView, boolean isChecked) {
        super.onChange(buttonView, isChecked);
    }

    @Override
    protected void saveSettings() {
        super.saveSettings();
        IntelligentLearningBean bean = new IntelligentLearningBean(
                getRoomUuid(),
                getRoomType(),
                getCurrentActivity().getSwitchState(),
                0,
                selectValue,
                0,
                0);
        LogUtils.d("睡眠超时", bean.toString());
        viewModel.saveData(bean, UrlApi.URL.SetSleepOvertime.getUrl());
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getCurrentActivity().setVipFunTip(R.string.room_sleep_timeout_tips);
        setTitle(R.string.room_sleep_timeout);

        init();
    }

    private void init(){
        timeMap = getTimeList(1f, 6f, 0.5f, getUnit());
        timeList = timeMap.keySet().toArray(new String[timeMap.size()]);

        //报警设置 报警时间
        viewDataBinding.roomAlarmLayout.setOnClickListener(v -> {
            BottomTextPickerDialog fragment = new BottomTextPickerDialog.Builder()
                    .setType(Type.NUMBER)
                    .setWheelItemTextSize(16)
                    .setDefValue(4)  //默认值（数组下标）
                    .setThemeColor(getResources().getColor(R.color.app_colorAccent))
                    .setIndexCallBack(new OnDateSetListener<Integer>() {
                        @Override
                        public void onDateSet(Fragment timePickerView, Integer sex) {
                            viewDataBinding.roomAlarmTime.setText(timeList[sex]);
                            selectValue = (int)(timeMap.get(timeList[sex]) * 60);
                            change();
                        }
                    })
                    .build(timeList);
            fragment.show(getChildFragmentManager(), BottomTextPickerDialog.class.getSimpleName());
        });


        RoomVipSetBean vipSettings = getVipSettings();
        if (vipSettings != null){
            getCurrentActivity().setSwitchChange(vipSettings.getFunctionSwitch() == 1);
            if(vipSettings.getFunctionAlert() == 0){
                // 失眠超时默认为3小时设定
                selectValue = 3 * 60;
                viewDataBinding.roomAlarmTime.setText("3" + getUnit());
                return;
            }
            selectValue = vipSettings.getFunctionAlert();
            viewDataBinding.roomAlarmTime.setText((vipSettings.getFunctionAlert() / 60f)+" " + getUnit());
        }
    }

    private String getUnit(){
        return " "+getString(R.string.room_hour);
    }
}
